﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Power.Models;
using Power.View.Dialog;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Power.ViewModel.Dialog
{
    public class ProjectManagementViewModel: BaseModel
    {
        private ProjectManagement model;
        public ProjectManagementViewModel(ProjectManagement model)
        {
            this.model = model;
            Close = new RelayCommand(OnClose);
            AddProject = new RelayCommand(OnAddProject);
            CheckBoxClick = new RelayCommand<int>(OnCheckBoxClick);
            Delete = new RelayCommand<int>(OnDelete);
            Data = new ObservableCollection<ProjectManaEntity>()
            {
                new ProjectManaEntity()
                {
                    ID = 1,
                    ProjectName = "项目1",
                    SubProject = "子项1",
                    Position = "地点1"
                },
                new ProjectManaEntity()
                {
                    ID = 2,
                    ProjectName = "项目2",
                    SubProject = "子项2",
                    Position = "地点2"
                },
                new ProjectManaEntity()
                {
                    ID = 3,
                    ProjectName = "项目3",
                    SubProject = "子项3",
                    Position = "地点3"
                }
            };
        }
        public void OnDelete(int id)
        {
            var item = Data.FirstOrDefault(t => t.ID == id);
            data.Remove(item);
        }
        private void OnCheckBoxClick(int obj)
        {
            var item = Data.Where(p => p.ID == obj).FirstOrDefault();
            if (item != null)
            {
                item.IsSelectedFlag = !item.IsSelectedFlag;
            }
            //var item = obj as PipManaEntity;
            //if (item != null && item.IsSelectedFlag)
            //{ 
            //    this.model.SectionDataGrid1.SelectedItems.Remove(item);
            //}
        }
        private void OnClose()
        {
            this.model.DialogResult = false;
            this.model.Close();
        }

        private void OnAddProject()
        {
            AddProject box = new AddProject();
            var result = box.ShowDialog();
        }

        private ObservableCollection<ProjectManaEntity> data;
        private ProjectManaEntity selectedItem;
        private bool checkBoxIsChecked;

        public bool CheckBoxIsChecked
        {
            get
            {
                return checkBoxIsChecked;
            }
            set
            {
                checkBoxIsChecked = value;
                ChangedSelecgedFlags();
                RaisePropertyChanged();
                RaisePropertyChanged("Data");
            }
        }

        public ProjectManaEntity SelectedItem
        {
            get
            {
                return selectedItem;
            }
            set
            {
                selectedItem = value;
                RaisePropertyChanged();
            }
        }

        public ObservableCollection<ProjectManaEntity> Data
        {
            get
            {
                return data;
            }
            set
            {
                data = value;
                RaisePropertyChanged();
            }
        }




        private void ChangedSelecgedFlags()
        {
            if (CheckBoxIsChecked == false)
            {
                foreach (var e in Data)
                    e.IsSelectedFlag = false;
            }
            else
            {
                foreach (var e in Data)
                    e.IsSelectedFlag = true;
            }
        }

        #region 
        public RelayCommand Close { get; private set; }
        public RelayCommand<int> CheckBoxClick { get; private set; }
        public RelayCommand<int> Delete { get; private set; }
        public RelayCommand AddProject { get; private set; }
        #endregion
    }
}
